package com.yu.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "EncodingFilter", value = "/*")
public class EncodingFilter implements Filter {
    // 初始化
    public void init(FilterConfig config) throws ServletException {
    }

    // 销毁，资源回收
    public void destroy() {
    }

    // 业务逻辑处理
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        // 将当前的请求，传递给后面的filter或者服务器
        chain.doFilter(request, response); // 不过调用该方法，请求会停留在当前过滤器中
    }
}
